pacman::p_load(readxl, gifski, gapminder,
plotly, gganimate, tidyverse)Hands-on Exercise 3 Part 2
4.1 Programming Animated Statistical Graphics with R
Creating animated data visualisation by using gganimate and plotly r packages
Learn how to reshape data by using tidyr package
Process, wrangle and transform data by using dplyr package
4.1.1 Basic Concepts of animation
Plots do not move
Many individual plots are built and stitched together as movie frames
Each frame is a different plot and subset drives the flow of the animation when stitched back together
4.1.2 Terminology
Frame: Represents a different point in time or a different category
Animation Attributes: Settings that control how the animation behaves
4.2 Getting Started
4.2.1 Loading the R packages
4.2.2 Importing the data
col <- c("Country", "Continent")
globalPop <- read_xls("GlobalPopulation.xls",
sheet="Data") %>%
mutate_at(col, as.factor) %>%
mutate(Year = as.integer(Year))4.3 Animated Data Visualisation: gganimate methods
gganimate extends the grammar of graphics as implemented by ggplot2 to include the description of animation
transition_*() - how data should be spread out and how it relates to itself across time
view_*() - how the positional scales should change along the animation
shadow_*() - how data from other points in time should be presented in the given point in time
enter_*()/exit_*() - how new data should appear and how old data should disappear during the course of the animation
ease_aes() - how different aesthetics should be eased during transitions
4.3.1 Building a static population bubble plot
ggplot(globalPop, aes(x = Old, y = Young,
size = Population,
colour = Country)) +
geom_point(alpha = 0.7,
show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}',
x = '% Aged',
y = '% Young') 
4.3.2 Building the animated bubble plot
Transition_time() of gganimate is used to create transition through distinct states in time (i.e. Year)
ease_aes() is used to control easing of aesthetics: Default is linear. Other methods are: quadratic, cubic, quartic, quintic, sine, circular, exponential, elastic, back and bounce
ggplot(globalPop, aes(x = Old, y = Young,
size = Population,
colour = Country)) +
geom_point(alpha = 0.7,
show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}',
x = '% Aged',
y = '% Young') +
transition_time(Year) +
ease_aes('linear') 
4.4 Animated Data Visualisation: plotly
both ggplotly() and plot_ly() support key frame animations through the frame argument/aesthetic
Also support an ids argument/aesthetic to ensure smooth transitions between objects with the same if
4.4.1 Building an animated bubble plot: ggplotly() method
gg <- ggplot(globalPop,
aes(x = Old,
y = Young,
size = Population,
colour = Country)) +
geom_point(aes(size = Population,
frame = Year),
alpha = 0.7,
show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(x = '% Aged',
y = '% Young')
ggplotly(gg)Even though show.legend = FALSE argument was used, the legend still appears on the plot. To overcome this problem, theme(legend.position=‘none’) should be used.
gg <- ggplot(globalPop,
aes(x = Old,
y = Young,
size = Population,
colour = Country)) +
geom_point(aes(size = Population,
frame = Year),
alpha = 0.7) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(x = '% Aged',
y = '% Young') +
theme(legend.position='none')
ggplotly(gg)4.4.2 Building an animated bubble plot: plot_ly() method
bp <- globalPop %>%
plot_ly(x = ~Old,
y = ~Young,
size = ~Population,
color = ~Continent,
sizes = c(2, 100),
frame = ~Year,
text = ~Country,
hoverinfo = "text",
type = 'scatter',
mode = 'markers'
) %>%
layout(showlegend = FALSE)
bp